/**
 * 向腾讯发送请求，获取疫情数据
 */
function getData() {
    $.ajax({
        url: 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5',
        dataType: 'jsonp',
        success: function (res) {
            data = JSON.parse(res.data)
            $("#confirm").html(data.chinaTotal.confirm);
            $("#heal").html(data.chinaTotal.heal);
            $("#dead").html(data.chinaTotal.dead);
            $("#nowConfirm").html(data.chinaTotal.nowConfirm);
            $("#noInfect").html(data.chinaTotal.noInfect);
            $("#importedCase").html(data.chinaTotal.importedCase);
            // console.log(data);
            var area = data.areaTree[0].children;//每个地区



            //中2地图
            var dataArea = [];
            for (var i = 0; i < area.length; i++) {
                var areaName = {
                    name: area[i].name,
                    value: area[i].total.confirm
                }
                dataArea.push(areaName);
            }
            var myChartCenter2 = echarts.init(document.getElementsByClassName("center2")[0],"dark");
            var optionCenter2 = {
                title: {
                    text: '',
                },
                tooltip: {
                    trigger: 'item'
                },
                visualMap: { // 左侧小导航图标
                    show: true,
                    x: 'left',
                    y: 'bottom',
                    textStyle: {
                        fontSize: 8,
                    },

                    splitList: [
                        { start: 1, end: 9 },
                        { start: 10, end: 99 },
                        { start: 100, end: 999 },
                        { start: 1000, end: 9999 },
                        { start: 10000 }
                    ],
                    color: ['#8A3310', '#C64918', '#E55B25', '#F2AD92', '#F9DCD1']
                },
                series: [{
                    name: '累计确诊人数',
                    type: 'map',
                    mapType: 'china',
                    roam: false, // 禁用拖动和缩放
                    itemStyle: { // 图形样式
                        normal: {
                            borderWidth: .5, //区域边框宽度
                            borderColor: '#009fe8', //区域边框颜色
                            areaColor: "#ffefd5", //区域颜色
                        },
                        emphasis: { // 鼠标滑过地图高亮的相关设置
                            borderWidth: .5,
                            borderColor: '#4b0082',
                            areaColor: "#fff",
                        }
                    },
                    label: { // 图形上的文本标签
                        normal: {
                            show: true, //省份名称
                            fontSize: 11,
                        },
                        emphasis: {
                            show: true,
                            fontSize: 16,
                            color:'black'
                        }
                    },
                    data: dataArea
                }]
            };
            myChartCenter2.setOption(optionCenter2);

            //右1
            function compare(property) {
                return function (a, b) {
                    var value1 = a[property];
                    var value2 = b[property];
                    return value2 - value1
                }
            }
            var List = dataArea.sort(compare("value"));
            var top10 = List.slice(0, 10);
            var top10Name = [];
            var top10Confrim = [];
            for (var i = 0; i < top10.length; i++) {
                top10Name.push(top10[i].name)
                top10Confrim.push(top10[i].value)
            }
            var myChartRight1 = echarts.init(document.getElementsByClassName("right1")[0],"dark");
            var optionRight1 = {
                title: {
                    text: "全国确诊省市TOP10",
                    textStyle: {
                        color: 'white',
                    },
                    left: 'left'
                },
                color: ['#3398DB'],
                tooltip: {
                    trigger: 'axis',
                    //指示器
                    axisPointer: {
                        type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
                    }
                },
                xAxis: {
                    type: 'category',
                    data: top10Name // ['湖北','广州','北京']
                },
                yAxis: {
                    type: 'value',
                    //y轴字体设置
                    axisLabel: {
                        show: true,
                        color: 'white',
                        fontSize: 12,
                        formatter: function (value) {
                            if (value >= 1000) {
                                value = value / 1000 + 'k';
                            }
                            return value;
                        }
                    },
                },
                series: [{
                    data: top10Confrim, // [582, 300, 100]
                    type: 'bar',
                    barMaxWidth: "50%"
                }]
            };
            myChartRight1.setOption(optionRight1);




            //右2
            var Top5 = [];
            for (var i = 0; i < area.length; i++) {
                var Top5Num = parseInt(area[i].today.tip.replace(/[^\d]/g, ' '))
                if (!Top5Num) {
                    Top5Num = 0;
                }
                var Top5Name = area[i].name
                var TOP5 = {
                    value: Top5Num,
                    name: Top5Name,
                }
                Top5.push(TOP5);//将对象存入数组
            }
            // console.log(Top5);//每个省的境外输入
            function compare1(property) {
                return function (a, b) {
                    var value1 = a[property];
                    var value2 = b[property];
                    return value2 - value1
                }
            }
            var Top5 = Top5.sort(compare1("value"));
            var top5 = Top5.slice(0, 5);
            // console.log(top5);
            var top5Name = [];
            var top5Confrim = [];
            for (var i = 0; i < top5.length; i++) {
                top5Name.push(top5[i].name)
                top5Confrim.push(top5[i].value)
            }

            var myChartRight2 = echarts.init(document.getElementsByClassName("right2")[0],"dark");
            var optionRight2 = {
                title: {
                    text: '境外输入省市TOP5',
                    left: 'center',
                    textStyle: {
                        color: 'white'
                    }
                },
                tooltip: {
                    trigger: 'item',
                    formatter: '{a} <br/>{b} : {c} ({d}%)'
                },
                legend: {
                    orient: 'vertical',
                    left: 'left',
                    data: top5Name,
                    textStyle: {
                        color: 'white'
                    }
                },
                series: [
                    {
                        name: '省市名称',
                        type: 'pie',
                        radius: '55%',
                        center: ['50%', '60%'],
                        data: top5,
                        emphasis: {
                            itemStyle: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };
            myChartRight2.setOption(optionRight2);

        }
    });

    $.ajax({
        url: 'https://api.inews.qq.com/newsqa/v1/query/inner/publish/modules/list?modules=chinaDayList,chinaDayAddList,cityStatis,nowConfirmStatis,provinceCompare',
        dataType: 'json',
        success: function (res) {
            console.log(res.data);
            // console.log(res.data.chinaDayList[0]);
            
            //左1
            var day=[];//日期
            var DayConfirm=[];//累计确证
            var DayHeal=[];
            var DayDead=[];
            var chinaDayList=res.data.chinaDayList;
            for(var i=0;i<chinaDayList.length;i++){
                DayConfirm.push(chinaDayList[i].confirm);
                day.push(chinaDayList[i].date);
                DayHeal.push(chinaDayList[i].heal);
                DayDead.push(chinaDayList[i].dead);
            }
            var myChartLeft1 = echarts.init(document.getElementsByClassName("left1")[0],"dark");
            var optionLeft1 = {
                title: {
                    text: "全国累计趋势",
                    textStyle: {
                        color: 'white',
                    },
                    left: 'left',
                },
                tooltip: {
                    trigger: 'axis',
                    //指示器
                    axisPointer: {
                        type: 'line',
                        lineStyle: {
                            color: '#7171C6'
                        }
                    },
                },
                //图例
                legend: {
                    data: ['累计确诊', "累计治愈", "累计死亡"],
                    left: "right",
                    textStyle:{
                        color: 'white',
                    }
                },
                //图形位置
                grid: {
                    left: '4%',
                    right: '6%',
                    bottom: '4%',
                    top: 50,
                    containLabel: true
                },
                xAxis: [{
                    type: 'category',
                    data: day //['03.20', '03.21', '03.22']
                }],
                yAxis: [{
                    type: 'value',
                    //y轴字体设置
                    axisLabel: {
                        show: true,
                        color: 'white',
                        fontSize: 12,
                        formatter: function (value) {
                            if (value >= 1000) {
                                value = value / 1000 + 'k';
                            }
                            return value;
                        }
                    },
                    //y轴线设置显示
                    axisLine: {
                        show: true
                    },
                    //与x轴平行的线样式
                    splitLine: {
                        show: true,
                        lineStyle: {
                            color: '#17273B',
                            width: 1,
                            type: 'solid',
                        }
                    }
                }],
                series: [{
                    name: "累计确诊",
                    type: 'line',
                    smooth: true,
                    data: DayConfirm
                }, {
                    name: "累计治愈",
                    type: 'line',
                    smooth: true,
                    data: DayHeal
                }, {
                    name: "累计死亡",
                    type: 'line',
                    smooth: true,
                    data: DayDead
                }]
            };
            myChartLeft1.setOption(optionLeft1)



            //左2
            var day2=[];
            var DayAddConfirm=[];
            var DayAddSuspect=[];
            var chinaDayAddList=res.data.chinaDayAddList;
            for(var i=0;i<chinaDayAddList.length;i++){
                day2.push(chinaDayAddList[i].date);
                DayAddConfirm.push(chinaDayAddList[i].confirm);
                DayAddSuspect.push(chinaDayAddList[i].suspect);
            }
            var myChartLeft2 = echarts.init(document.getElementsByClassName("left2")[0],"dark");
            var optionLeft2 = {
                title: {
                    text: '全国新增趋势',
                    textStyle: {
                        color: 'white',
                    },
                    left: 'left',
                },
                tooltip: {
                    trigger: 'axis',
                    //指示器
                    axisPointer: {
                        type: 'line',
                        lineStyle: {
                            color: '#7171C6'
                        }
                    },
                },
                //图例
                legend: {
                    data: ['新增确诊', '新增疑似'],
                    left: 'right',
                    textStyle: {
                        color: 'white',
                    },
                },
                //图形位置
                grid: {
                    left: '4%',
                    right: '6%',
                    bottom: '4%',
                    top: 50,
                    containLabel: true
                },
                xAxis: [{
                    type: 'category',
                    data: day2
                }],
                yAxis: [{
                    type: 'value',
                    //y轴字体设置
                    axisLabel: {
                        show: true,
                        color: 'white',
                        fontSize: 12,
                        formatter: function(value) {
                            if (value >= 1000) {
                                value = value / 1000 + 'k';
                            }
                            return value;
                        }
                    },
                    //y轴线设置显示
                    axisLine: {
                        show: true
                    },
                    //与x轴平行的线样式
                    splitLine: {
                        show: true,
                        lineStyle: {
                            color: '#17273B',
                            width: 1,
                            type: 'solid',
                        }
                    }
                }],
                series: [{
                    name: '新增确诊',
                    type: 'line',
                    smooth: true,
                    data: DayAddConfirm
                }, {
                    name: '新增疑似',
                    type: 'line',
                    smooth: true,
                    data: DayAddSuspect
                }]
            };
            
            myChartLeft2.setOption(optionLeft2);
        }
    });





}
getData();
//时间
function show() {
    var today = new Date();
    var year = today.getFullYear();
    var month = today.getMonth();
    var date = today.getDate();
    var hours = today.getHours();
    var minutes = today.getMinutes();
    var seconds = today.getSeconds();
    var day = today.getDay();//1
    var arrs = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
    var noon = "AM";
    if (minutes < 10) {
        minutes = "0" + minutes;
    }
    if (seconds < 10) {
        seconds = "0" + seconds;
    }
    // if(hours>12){
    //     noon="PM";
    //     hours=hours-12;
    // }
    document.getElementsByClassName("time")[0].innerHTML = year + "年" + (month + 1) + "月" + date + "日" + "  " + hours + ":" + minutes + ":" + seconds;

}
window.onload = function () {
    show();
    setInterval(show, 1000);
};





